/*
8. Flush Straight
7. Four of a kind
6. Full House
5. Flush
4. Straight
3. 3 of a kind
2. 2 Pairs
1. Pair
0. High card
*/


function Card(cardValue, color) {
  this.cardValue = cardValue;
  this.color = color;
}

function helloWorld(name) {
  $('body').html('Hello ' + name + '<br/>');
}
var nameOfUser = 'Pok';
// helloWorld(nameOfUser);

var colorsS = ['&diams;', '&spades;', '&hearts;', '&clubs;'];
var colors = ['D', 'S', 'H', 'C'];
var cardsValue = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'];
var handsName = ['High card', 'Pair', '2 Pairs', '3 of a kind', 'Straight', 'Flush', 'Full House', 'Four of a kind', 'Flush Straight'];


/**
 * Generates Hands
 */
function generateHand() {
  var hands = [];
  for (var i = 0; i < 5; i++) {
    var color = colors[randomInt(colors.length)];
    var cardValue = cardsValue[randomInt(cardsValue.length)];
    hands[i] = new Card(cardValue, color);
  }
  
  displayTable(hands);

}
//generateHand();

/**
 * Displays a table with an html list on the body.
 * @param table Table we want displayed.
 */
function displayHands(hands) {
  var ulElement = $('<ul></ul>');
  for (var i = 0; i < hands.length; i++ ) {
    ulElement.append('<li>'+ cardsValue[hands[i].cardValue] + hands[i].color +'</li>');
  }
  $('body').append(ulElement)
}

/**
 * Generates a random integer.
 * @param max The maximum of the random number.
 * @returns The random number.
 */
function randomInt (max) {  
  return Math.floor(Math.random()*max);
}

/**
 * Returns all cards of a packet.
 */
function getAllCards () {
  var cards = [];
  for (var i = 0; i < colors.length; i++) {
    for (var j = 0; j < cardsValue.length; j++) {
      cards.push(new Card(j, colors[i]));
    }
  }
  return cards;
}

/**
 * Shuffles an array.
 * @param array Array to shuffle.
 */
function shuffleTable(tab) {
  for (var i = 0; i < tab.length; i++) {
    tab = swap(tab, i, randomInt(tab.length))
  }
  return tab;
}

function swap(tab, index1, index2) {
  var temp = tab[index1];
  tab[index1] = tab[index2];
  tab[index2] = temp;
  return tab;
}



/**
 * Shuffles an array.
 * @param deck All hands.
 * @param maxCards Number of cards we want. 
 */
function getCards(deck, maxCards) {
  var newCards = [];
  for (var i = 0; i < maxCards; i ++) {
  	newCards[i] = deck[i];  	
  }
  return newCards;
}




/**
 * Generates all turns possible.
 * @param cards 5 cards composed from preflop and flop.
 * @return all turns possible. 
 */
function generateAllTurns(deck, cards) {
//  var combinations = getCombinationsCards(deck.);
  var allTurns = [];
  for (var i = 5; i < deck.length; i++) {
    var newTurn = cards.slice(0)
    newTurn.push(deck[i]);
    allTurns.push(newTurn);
  }
  return allTurns;
}


function isInDeck(card, deck) {
  for (var i = 0; i < deck.length; i++) {
    if (deck[i] == card) {
	  return true;
	}
  }
  return false;
}
// combination



/**
 * Returns combinations for turn.
 */
function combinateTurns(cards) {
  var combinations = [];
  for (var i = 0; i < cards.length; i++) {
    combinations[i] = (cards.slice(0));
	combinations[i].splice(i, 1);
  }
  return combinations;
}

//var combs = combinateTurns(turn1);



function returnBestValue(hands) {
  var bestValue = getHandValue(returnBestHand(hands[0], hands[1]));
  for (var i = 2; i < hands.length; i++) {
    var bestValue = returnBestValueForTwoHandsValues(bestValue, getHandValue(hands[i]));
  }
  return bestValue;
}




$(document).ready(function() {

})

function displayTurnResult() {
  var deck = getAllCards();
  deck = shuffleTable(deck);
  var cards = getCards(deck, 5);
  
  cards.sort(function(a, b) {return b.cardValue - a.cardValue});

  $('body').append('Flop : ');
  displayHands(cards);
  $('body').append('All outs : <span id="allOuts"></span><br/><span id="probaHands"></span><br/>');
  appendBody(getHandValue(cards));
  calculateOutTurn(deck, cards);
}


function calculateOutTurn(deck, cards) {
  var flopValue = getHandValue(cards);
  var allTurns = generateAllTurns(deck, cards);
  var probaHands = []; 
  var outs = 0;
  for (var i = 0; i < allTurns.length; i++) {
    var bestValueTurn = returnBestValue(combinateTurns(allTurns[i]));
    if (returnBestValueForTwoHandsValues(bestValueTurn, flopValue) != flopValue && bestValueTurn[0] != flopValue[0]) {
	  displayHands(allTurns[i]);
	  appendBody(bestValueTurn);
	  if (probaHands[bestValueTurn[0]] == undefined) {
	  	probaHands[bestValueTurn[0]] = 1;
	  } else {
	    probaHands[bestValueTurn[0]]++;
	  }
	  outs ++;
	}
  }
  if (flopValue[0] > 1)alert(4)
  $('#allOuts').html(' nombre outs : ' + outs + '<br/>cote : ' + Math.round(47 *10/outs )/10 + ' contre 1<br/><br/>');
  displayProbaHands(probaHands);
}

function displayProbaHands(probaHands) {
  var probaText = '';
  for (var i = 0, probaHandsLength = probaHands.length; i < probaHandsLength; i++) {
  	if (probaHands[i] != undefined) {
  	  probaText += 'La proba davoir - ' + handsName[i] + ' est ' + Math.round(probaHands[i]*100/47) + '% avec ' + probaHands[i] + 'outs<br/>';	
  	}
  }
  $('#probaHands').html(probaText);
}



function appendBody(text) {
  $('body').append('getHandValue : ' + text + '<br/><br/>');
}


function getCombinations(chars, combSize) {
  var result = [];
  var f = function(prefix, chars) {
    for (var i = 0; i < chars.length; i++) {
      if (prefix.length == combSize - 1) {
        result.push(prefix + chars[i]);
      }
      f(prefix + chars[i], chars.slice(i + 1));
    }
  }
  f('', chars);
  return result;
}

/**
 * Gets all combination from a set of cards.
 */
function getCombinationsCards(cardsCounts, combSize) {
  var chars = []
  for (var i = 0; i < cardsCounts; i++) {
  	chars.push(i);
  } 
  return getCombinations(chars, combSize);
}


//combinations = getCombinationsCards(7, 5);
//appendBody(combinations);












